package com.qingmeng.project.controller;

import com.google.protobuf.ServiceException;
import com.qingmeng.apicommon.common.BaseResponse;
import com.qingmeng.apicommon.common.ResultUtils;
import com.qingmeng.apicommon.model.entity.User;
import com.qingmeng.apicommon.utils.StringUtils;
import com.qingmeng.project.annotation.Anonymous;
import com.qingmeng.project.model.dto.loginuser.LoginBody;
import com.qingmeng.project.model.dto.RegisterBody;
import com.qingmeng.project.service.SysLoginService;
import com.qingmeng.project.service.SysRegisterService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@Api(tags = "用户登录接口")
public class UserLoginController {


    @Resource
    private SysLoginService loginService;

    @Resource
    private SysRegisterService registerService;


    /**
     * 用户登录接口
     * @param loginBody
     * @return
     */
    @PostMapping("/login")
    @Anonymous
    @Operation(summary = "用户登录", description = "用户登录")
    public BaseResponse<User> userLogin(
            @Parameter(description = "用户登录对象") @RequestBody LoginBody loginBody) throws ServiceException {
        BaseResponse<User> success = ResultUtils.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(),
                loginBody.getUuid());
        return success;
    }

    @PostMapping("/register")
    @Anonymous
    @Operation(summary = "用户注册", description = "用户注册")
    public BaseResponse<User> register(
            @Parameter(description = "用户注册对象") @RequestBody RegisterBody user)
    {
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? ResultUtils.success() : ResultUtils.error(msg);
    }
}
